const search = require('../../lib/search.js');
var rules = {
    type: null,
    type2: null,
    type3: null,
    type4: null,
    attribute: null,
    race: null,
    level: null,
    attack_range: null,
    defend_range: null
};

Page({
    data: {
        key: "",
        rule_t: false,
        list_t: true,
        first: true,
        index: {
            type: 0,
            types: [0, 0, 0],
            attribute: 0,
            race: 0,
            level: 0,
            al: null,
            au: null,
            dl: null,
            du: null
        },
        ruleList: {
            type: ['无', '怪兽', '魔法', '陷阱'],
            types: null,
            attribute: ['无', '光', '暗', '风', '水', '炎', '地', '神'],
            race: ["无", "龙族", "战士族", "恶魔族", "魔法师族", "天使族", "不死族", "岩石族", "植物族", "昆虫族", "水族", "炎族", "雷族", "鱼族", "海龙族", "幻龙族", "恐龙族", "爬虫族", "机械族", "兽族", "鸟兽族", "兽战士族", "念动力族", "电子界族", "幻神兽族", "创造神族"],
            level: ["无", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 13]
        }
    },
    onLoad: function (options) {
        const rl = new search.Rule({});
        let parse = rl.unzip(options);
        let keywords = parse.keywords;
        rules = parse.rule;

        let key = keywords ? keywords : '';
        this.setData({ key });
        this.search();
    },
    onReady: function () {

    },
    onShow: function () {

    },
    onHide: function () {
        clearTimeout();
    },
    onUnload: function () {

    },
    onPullDownRefresh: function () {

    },
    onReachBottom: function () {
        if (this.data.rule_t) {
            return;
        }
        const component = this.selectComponent("#result");
        if (component.data.tip == "已经加载全部") {
            return;
        }
        if (this.data.list_t) {
            this.search(false);
        }
    },
    onShareAppMessage: function () {

    },
    keyChange(e) {
        this.setData({ key: e.detail.value });
    },
    search(reset = true) {
        const that = this;
        const component = this.selectComponent("#result");

        wx.setNavigationBarTitle({
            title: '正在查询 ',
        })
        that.setData({
            list_t: true,
            rule_t: false
        })
        wx.showNavigationBarLoading();
        component.iter({
            keywords: that.data.key,
            rule:rules,
            success(res) {
                that.clear_rules();
                that.setData({
                    list_t: true
                })
                wx.setNavigationBarTitle({
                    title: '',
                })
                wx.hideNavigationBarLoading();
            },
            fail() {
                wx.showToast({
                    title: '没有搜索到(┬＿┬)',
                    icon: 'none',
                    duration: 1500
                })
                wx.setNavigationBarTitle({
                    title: '',
                })
                wx.hideNavigationBarLoading();
            },
            reset
        })
    },
    changeData: function (list) {
        var prop1 = ['light', 'dark', 'wind', 'water', 'fire', 'ground', 'magic', 'tragic', 'god'];
        var prop2 = ['光', '暗', '风', '水', '炎', '地', '魔', '罠', '神'];
        for (var i = 0; i < list.length; i++) {
            var new_prop = prop2[prop1.indexOf(list[i].attribute)];
            list[i].attribute = new_prop;
            var txt = this.race_text(list[i].type, list[i].type2, list[i].type3, list[i].type4, list[i].race);
            list[i].race_text = txt;
        }
        return list;
    },
    changeType: function (res) {
        var ruleList = this.data.ruleList;
        var index = this.data.index;
        index.type = res.detail.value;
        if (res.detail.value == 0) {
            rules.type = null;
            rules.type2 = null;
            rules.type3 = null;
            rules.type4 = null;
            rules.attribute = null;
            rules.race = null;
            rules.level = null;
            rules.attack_range = null;
            rules.defend_range = null;
        }
        else if (res.detail.value == 1) {
            rules.type = 'monster';
            rules.type2 = null;
            rules.type3 = null;
            rules.type4 = null;
            rules.attribute = null;
            rules.race = null;
            rules.level = null;
            rules.attack_range = null;
            rules.defend_range = null;
            ruleList.types = [
                ['无', '通常', '效果', '仪式', '融合', '同调', '超量', '连接'],
                ['无', '通常', '调整', '灵魂', '二重', '同盟', '反转', '卡通', '特殊召唤', '灵摆'],
                ['无', '通常', '调整', '灵魂', '二重', '同盟', '反转', '卡通', '特殊召唤'],
            ]
        }
        else if (res.detail.value == 2) {
            rules.type = 'magic';
            rules.type3 = null;
            rules.type4 = null;
            rules.attribute = null;
            rules.race = null;
            rules.level = null;
            rules.attack_range = null;
            rules.defend_range = null;
            ruleList.types = [
                ['无', '通常', '永续', '速攻', '装备', '场地', '仪式']
            ]
        }
        else {
            rules.type = 'tragic';
            rules.type3 = null;
            rules.type4 = null;
            rules.attribute = null;
            rules.race = null;
            rules.level = null;
            rules.attack_range = null;
            rules.defend_range = null;
            ruleList.types = [
                ['无', '通常', '永续', '反击']
            ]
        }
        this.setData({
            ruleList: ruleList,
            index: index
        })
    },
    hide_list() {
        const that=this;
        this.setData({
            rule_t: true
        });
        setTimeout(function() {
            that.setData({
                list_t: false
            })
        },300)
    },
    show_list() {
        const that = this;
        this.setData({
            list_t: true
        });
        setTimeout(function () {
            that.setData({
                rule_t: false
            })
        }, 10)
    },
    showRules: function () {
        if (!this.data.rule_t) {
            this.hide_list();
        }
        else {
            this.show_list();
        }
        this.setData({
            first: false
        })
    },
    changeType2: function (res) {
        var types = res.detail.value;
        if (this.data.index.type == 1) {
            var typeList = [null, 'tc', 'xg', 'ys', 'rh', 'tt', 'cl', 'lj'];
            var typeList2 = [null, 'tc', 'tz', 'lh', 'ec', 'tm', 'fz', 'kt', 'sm', 'lb'];
            var type2 = typeList[types[0]];
            var type3 = typeList2[types[1]];
            var type4 = typeList2[types[2]];
            var index = [types[0], types[1], types[2]]
        }
        else if (this.data.index.type == 2) {
            var typeList = [null, 'tc', 'yx', 'sg', 'zb', 'cd', 'ys'];
            var type2 = typeList[types[0]];
            var type3 = null, type4 = null;
            var index = [types[0], 0, 0]
        }
        else if (this.data.index.type == 3) {
            var typeList = [null, 'tc', 'yx', 'fj'];
            var type2 = typeList[types[0]];
            var type3 = null, type4 = null;
            var index = [types[0], 0, 0]
        }

        var index0 = this.data.index;
        rules.type2 = type2;
        rules.type3 = type3;
        rules.type4 = type4;
        index0.types = index;
        this.setData({
            index: index0
        })
    },
    changeAttribute: function (res) {
        var attributeList = [null, 'light', 'dark', 'wind', 'water', 'fire', 'ground', 'god', 'magic', 'tragic'];
        var attribute = attributeList[res.detail.value];
        var index = this.data.index;
        rules.attribute = attribute;
        index.attribute = res.detail.value;
        this.setData({
            index: index
        })
    },
    changeRace: function (res) {
        var raceList = [null, "龙族", "战士族", "恶魔族", "魔法师族", "天使族", "不死族", "岩石族", "植物族", "昆虫族", "水族", "炎族", "雷族", "鱼族", "海龙族", "幻龙族", "恐龙族", "爬虫族", "机械族", "兽族", "鸟兽族", "兽战士族", "念动力族", "电子界族", "幻神兽族", "创造神族"];
        var race = raceList[res.detail.value];
        var index = this.data.index;
        rules.race = race;
        index.race = res.detail.value;
        this.setData({
            rules: rules,
            index: index
        })
    },
    changeLevel: function (res) {
        var levelList = [null, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 13];
        var level = levelList[res.detail.value];
        var index = this.data.index;
        rules.level = level;
        index.level = res.detail.value;
        this.setData({
            index: index
        })
    },
    changeAL: function (res) {
        var al = res.detail.value;
        var index = this.data.index;
        index.al = al;
        if (!rules.attack_range) {
            if (al == "") {
                var ar = null;
            }
            else {
                var ar = [al, 5000];
            }
        }
        else {
            var up = rules.attack_range[1];
            if (al == "") {
                var ar = [0, 5000]
            }
            else {
                var ar = [al, up];
            }
        }
        rules.attack_range = ar;
        this.setData({
            index: index
        })
    },
    changeAU: function (res) {
        var au = res.detail.value;
        var index = this.data.index;
        index.au = au;
        if (!rules.attack_range) {
            if (au == "") {
                var ar = null;
            }
            else {
                var ar = [0, au];
            }
        }
        else {
            var low = rules.attack_range[0];
            if (au == "") {
                var ar = [0, 5000]
            }
            else {
                var ar = [low, au];
            }
        }
        rules.attack_range = ar;
        this.setData({
            index: index
        })
    },
    changeDL: function (res) {
        var dl = res.detail.value;
        var index = this.data.index;
        index.dl = dl;
        if (!rules.defend_range) {
            if (dl == "") {
                var dr = null;
            }
            else {
                var dr = [dl, 5000];
            }
        }
        else {
            var up = rules.defend_range[1];
            if (dl == "") {
                var dr = [0, 5000]
            }
            else {
                var dr = [dl, up];
            }
        }
        rules.defend_range = dr;
        this.setData({
            index: index
        })
    },
    changeDU: function (res) {
        var du = res.detail.value;
        var index = this.data.index;
        index.du = du;
        if (!rules.defend_range) {
            if (du == "") {
                var dr = null;
            }
            else {
                var dr = [0, du];
            }
        }
        else {
            var low = rules.defend_range[0];
            if (du == "") {
                var dr = [0, 5000]
            }
            else {
                var dr = [low, du];
            }
        }
        rules.defend_range = dr;
        this.setData({
            index: index
        })
    },
    clear_rules: function () {
        this.setData({
            rule: {
                type: null,
                type2: null,
                type3: null,
                type4: null,
                attribute: null,
                race: null,
                level: null,
                attack_range: null,
                defend_range: null
            },
            rule_t: false,
            index: {
                type: 0,
                types: [0, 0, 0],
                attribute: 0,
                race: 0,
                level: 0,
                al: null,
                au: null,
                dl: null,
                du: null
            }
        })
    },
})